
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

using OpenTK;
using OpenTK.OpenGL;
using OpenTK.OpenGL.Enums;
using OpenTK.Fonts;
using OpenTK.Math;
using OpenTK.Input;
using OpenTK.Platform;

namespace Gamadus.Engine
{
	public class Room : Resource
	{
		public bool persistent;
		private Color _bgcolor;
		public Color bgcolor
		{
			get
			{
				return _bgcolor;
			}
			set
			{
				if(WindowMgr.Instance.HasWindow)
				{
					GL.ClearColor(value);
				}
				_bgcolor = value;
			}
		}
		public int width;
		public int height;
		public int speed;
		public int room_speed
		{
			get
			{
				return speed;
			}
			set
			{
				
				WindowMgr.Instance.Fps = value;
				speed = value;
			}
		}
		string mcaption;
		public string caption
		{
			set
			{
				mcaption = value;
				WindowMgr.Instance.Title = (mcaption == "") ? " " : mcaption;
			}
			get
			{
				return mcaption;
			}
		}
		
		public List<Instance> Instances
		{
			get
			{
				return instances;
			}
		}
		
		List<Instance> instances = new List<Instance>();
		public Room(int id) : base(id)
		{
			width = 640;
			height = 480;
			speed = 30;
			persistent = false;
			bgcolor = Color.FromArgb(255,0,0,0);
		}
		public bool leaveRoom = false;
		public void GoTo()
		{
			GL.ClearColor(bgcolor);
			
			WindowMgr.Instance.ChangeVideoMode(width, height);
			RoomMgr.Instance.CurrentRoom.Fill();
			WindowMgr.Instance.Fps = speed;
			
			
		}
		
		public void Fill()
		{	
			if(!persistent || instances.Count < 1)
			{
				ResourceMgr.Instance.FillRoom(this);
			}
		}
		
		
		bool LeaveRoom
		{
			get
			{
				if(leaveRoom)
				{
					leaveRoom = false;
					return true;
				}
				else
					return false;
			}
		}
		public void RoomStep()
		{
			for(int i = 0; i < instances.Count; i++)
			{
				instances[i].Step();
			}
			for(int i = 0; i < instances.Count; i++)
			{
				instances[i].Draw();
			}	
		}
	}
}

//public partial class  GamadusRoom
//{
//			
//	int _width;
//	int _height;
//	int _speed;
//	public int _room_nr;
//	
//	public System.Collections.ArrayList instances = new System.Collections.ArrayList();
//	public System.Collections.ArrayList instancesToCreate = new System.Collections.ArrayList();
//	public System.Collections.ArrayList instancesToDelete = new System.Collections.ArrayList();
//	
//	public string Caption;
//	
////	int _next_room;
////	int _prev_room;
//	
////	public GamadusObject obj1;
////	public GamadusObject2 obj2;
////	public GamadusObject2 obj3;
//	
//	public Color background_color = Color.Gray;
//	public bool persistent = false;
//	int speed = 0;
//	public int Speed
//	{
//		set
//		{
//			speed = value;
//			SdlDotNet.Core.Events.Fps = speed;
//		}
//		get
//		{
//			return speed;
//		}
//	}
//	
//	public GamadusRoom(int width, int height, int speed, int room_nr)
//	{
//		if(width<120)
//			_width=120;
//		else
//			_width=width;
//		if(height<120)
//			_height=120;
//		else
//			_height=height;
//		
//		_speed=speed;
//		_room_nr=room_nr;
//		
//		//instances.Add(next_instance,obj1);
//		Caption="";
//	}
//	
//	public void GoTo()
//	{
//		if(!persistent)
//		Config.FillRoom(_room_nr);
//		SdlDotNet.Core.Events.Fps = _speed;
//		Window.SetVideoMode(_width, _height, Window.fullscreen);
//		
//		if(Global.current_room != null && !Global.current_room.persistent)
//		{
//			//clear current room
//			for(int i = 0; i < Global.current_room.instances.Count; i++)
//			{
//				Global.current_room.instances[i] = null;
//			}
//			Global.nonstaticContainer.FirstInstance.Clear();
//		}
//		Global.current_room = this;
//	}
//	
//	public void RoomStep()
//	{
//		Video.Screen.Fill(background_color);
//		
//		foreach(object instance in instancesToDelete)
//		{
//			int ind = instances.IndexOf(instance);
//			
//			instances[ind] = null;
//			instances.RemoveAt(ind);
//			string searchname = ((GamadusObjectParent)instance).name;
//			if(Global.nonstaticContainer.FirstInstance[searchname] == instance)
//			{
//				Global.nonstaticContainer.FirstInstance[searchname] = null;
//				foreach(GamadusObjectParent inst in instances)
//				{
//					//Console.WriteLine("searching "+searchname+" in "+inst.name);
//					if(inst.name == searchname)
//					{
//						Global.nonstaticContainer.FirstInstance[searchname] = inst;
//						break;
//					}
//					
//				}
//				//TODO: get next first instance
//			}
//		}
//		instancesToDelete.Clear();
//		
//		foreach(object instance in instancesToCreate)
//		{
//			instances.Add(instance);
//		}
//		instancesToCreate.Clear();
//		//Console.WriteLine(instances.Count.ToString());
//		
//		foreach(object instance in instances)
//		{
//			IGamadusObject obj = instance as IGamadusObject;
//			obj.ExecuteDraw();			
//		}
//		foreach(object instance in instances)
//		{
//			IGamadusObject obj = instance as IGamadusObject;
//			obj.ExecuteStep();			
//		}
//			
//		
//		Video.WindowCaption = Caption;
//			
//	}
//	
////	public string caption
////	{
////		get
////		{
////			return Video.WindowCaption;
////		}
////		set
////		{
////			Video.WindowCaption = value;
////		}
////	}
//	
//	public int width
//	{
//		get
//		{
//			return _width;
//		}
//	}
//	
//	public int height
//	{
//		get
//		{
//			return _height;
//		}
//	}
//}

